/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2013
 */

package org.teacup.eightdays.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.org.rapid_framework.web.scope.Flash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cn.org.rapid_framework.beanutils.BeanUtils;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import org.teacup.eightdays.model.*;
import org.teacup.eightdays.dao.*;
import org.teacup.eightdays.service.*;
import org.teacup.eightdays.vo.query.*;

/**
 * @author Abner email:521yexu2008(a)163.com
 * @version 1.0
 * @since 1.0
 */

@Controller
public class SchoolController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private SchoolManager schoolManager;
	
	private final String LIST_ACTION = "redirect:/School/list.shtml";
	
	public SchoolController() {
	}
	
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/**
	 * 执行搜索
	 * 
	 * @param request
	 * @param response
	 * @param query
	 * @return
	 */
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,SchoolQuery query) {
		Page page = this.schoolManager.findPage(query);
		
		ModelAndView result = new ModelAndView("/School/list");
		result.addAllObjects(toModelMap(page, query));
		return result;
	}
	
	/**
	 * 查看对象
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Long id = new java.lang.Long(request.getParameter("schoolId"));
		School school = (School)schoolManager.getById(id);
		return new ModelAndView("/School/show","school",school);
	}
	
	/**
	 * 进入新增页面
	 * 
	 * @param request
	 * @param response
	 * @param member
	 * @return
	 * @throws Exception
	 */
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,School school) throws Exception {
		return new ModelAndView("/School/create","school",school);
	}
	
	/**
	 * 保存新增对象
	 * 
	 * @param request
	 * @param response
	 * @param member
	 * @return
	 * @throws Exception
	 */
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,School school) throws Exception {
		schoolManager.save(school);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 * 进入更新页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Long id = new java.lang.Long(request.getParameter("schoolId"));
		School school = (School)schoolManager.getById(id);
		return new ModelAndView("/School/edit","school",school);
	}
	
	/**
	 * 保存更新对象
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Long id = new java.lang.Long(request.getParameter("schoolId"));
		
		School school = (School)schoolManager.getById(id);
		bind(request,school);
		schoolManager.update(school);
		Flash.current().success(UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 * 删除对象
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String itemId = request.getParameter("itemid");
		if (itemId!=null && !"".equals(itemId)) {
			java.lang.Long id = new java.lang.Long(itemId);
			schoolManager.removeById(id);
		}else {
			String[] items = request.getParameterValues("items");
			for(int i = 0; i < items.length; i++) {
				Hashtable params = HttpUtils.parseQueryString(items[i]);
				
				java.lang.Long id = new java.lang.Long((String)params.get("schoolId"));
				
				schoolManager.removeById(id);
			}
		}
		Flash.current().success(DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 * School-School.TABLE_ALIAS
	 * 
	 * @param mamager the School-School.TABLE_ALIAS to set 
	 */
	public void setSchoolManager(SchoolManager manager) {
		this.schoolManager = manager;
	}
	
}

